package emulator.unittest.hardware.nmos6502;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.memory.SparseMemory;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.stack.StackData;

public class Cpu6502Test {
	private Cpu6502 cpu;

	@Before
	public void setUp()	{
		cpu = new Cpu6502();
		cpu.attach(new MockMemory(), null);
		cpu.setPC(0);
		cpu.setSP(new HwByte(0x7F));
	}

	@Test
	public void testFetchProgramByte() throws EmulatorException {
		assertEquals(0, cpu.fetchProgramByte());
		assertEquals(1, cpu.getPC());
	}

	@Test
	public void testFetchProgramWord() throws EmulatorException {
		assertEquals(0x0100, cpu.fetchProgramWord());
		assertEquals(2, cpu.getPC());
	}
	
	@Test
	public void testReadByte() throws EmulatorException {
		assertEquals(0xCD, cpu.readByte(0x10CD));
	}

	@Test
	public void testReadWord() throws EmulatorException {
		assertEquals(0xCECD, cpu.readWord(0x10CD));
	}

	@Test
	public void testPushPop() throws EmulatorException {
		cpu.attach(new SparseMemory(), null);
		int target = 0x1234;
		cpu.pushAddress(target, StackData.SOURCE_UNKNOWN);
		assertEquals(target, cpu.popAddress());
	}
}
